package com.fishleap.speaker;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author zlg
 */
// 1.实现writable接口
public class SpeakBean implements Writable {

    private long selfDuration;
    private long thirdPartDuration;
    private long sumDuration;

    // 2.反序列化时，需要反射调用空参构造函数，所以必须有
    public SpeakBean() {
    }

    public SpeakBean(long selfDuration, long thirdPartDuration) {
        this.selfDuration = selfDuration;
        this.thirdPartDuration = thirdPartDuration;
        this.sumDuration = this.selfDuration + this.thirdPartDuration;
    }

    // 3.写序列化方法
    @Override
    public void write(DataOutput output) throws IOException {
        output.writeLong(selfDuration);
        output.writeLong(thirdPartDuration);
        output.writeLong(sumDuration);
    }

    // 4.反序列化方法：反序列化方法读顺序必须和写序列化方法的写顺序必须一致
    @Override
    public void readFields(DataInput input) throws IOException {
        this.selfDuration = input.readLong();
        this.thirdPartDuration = input.readLong();
        this.sumDuration = input.readLong();
    }

    // 5.编写toString方法，方便后续打印到文本
    @Override
    public String toString() {
        return selfDuration +
                "\t" + thirdPartDuration +
                "\t" + sumDuration;
    }

    public long getSelfDuration() {
        return selfDuration;
    }

    public void setSelfDuration(long selfDuration) {
        this.selfDuration = selfDuration;
    }

    public long getThirdPartDuration() {
        return thirdPartDuration;
    }

    public void setThirdPartDuration(long thirdPartDuration) {
        this.thirdPartDuration = thirdPartDuration;
    }

    public long getSumDuration() {
        return sumDuration;
    }

    public void setSumDuration(long sumDuration) {
        this.sumDuration = sumDuration;
    }
}
